home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 08 - 1992 / 08.06 Oct 92 / One-Application Patches / patcher WDEF.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-08  |  2.6 KB  |  118 lines  |  [TEXT/KAHL]

  1. /*    -------------------------------------------
  2.         patcher WDEF.c
  3.         
  4.         THINK C "Set Project Type..." settings:
  5.         code resource, type WDEF, ID 0,
  6.         custom header, preloaded,
  7.         file type 'rsrc', file creator 'RSED'.
  8.         -------------------------------------------
  9. */
  10. #include "defs.h"
  11.  
  12. pascal long main( short var_code,
  13.     WindowPeek the_window,
  14.     short message, long param );
  15. Boolean No_ResEdit_danger( void );
  16.  
  17. /* The one and only global variable */
  18. static Boolean        run_needed = true;
  19.  
  20. pascal long main( short var_code,
  21.     WindowPeek the_window,
  22.     short message, long param )
  23. {
  24.     long                retval;
  25.     Handle            real_WDEF_h;
  26.     short                save_resfile;
  27.     SignedByte    real_WDEF_state;
  28.     WDEF_proc    Real_WDEF;
  29.     Ptr                    save_A4;
  30.     Handle            code_h;
  31.     short                res_index;
  32.     OAPn_proc    OAPn_p;
  33.     OAPd_proc    OAPd_p;
  34.     THz                    save_zone;
  35.     
  36.     asm {
  37.         move.L        A4, save_A4
  38.         LEA                main, A4        ; for access to global
  39.     }
  40.  
  41.     save_resfile = CurResFile();
  42.     UseResFile( SysMap );
  43.     real_WDEF_h = RGetResource( 'WDEF', 0 );
  44.     real_WDEF_state = HGetState( real_WDEF_h );
  45.     HLock( real_WDEF_h );
  46.     Real_WDEF = (WDEF_proc)
  47.                                     StripAddress(*real_WDEF_h);
  48.     UseResFile( save_resfile );
  49.     
  50.     /* Here's where we call the real system WDEF */
  51.     retval = Real_WDEF( var_code, the_window,
  52.                     message, param );
  53.     HSetState( real_WDEF_h, real_WDEF_state );
  54.  
  55.     if (No_ResEdit_danger())
  56.     {
  57.         save_zone = GetZone();
  58.         SetZone( ApplicZone() );
  59.     
  60.         if ( (message == wNew) && run_needed )
  61.         {
  62.             for (res_index = 1; ; ++res_index)
  63.             {
  64.                 code_h = GetIndResource('OAPn', res_index);
  65.                 if (code_h == NIL)
  66.                     break;
  67.                 HLock( code_h );
  68.                 OAPn_p = (OAPn_proc) StripAddress(*code_h);
  69.                 (*OAPn_p)();
  70.             }
  71.             run_needed = false;
  72.         }
  73.         
  74.         else if ( (message == wDraw) &&    // draw...
  75.                     (LoWord(param) == 0) &&    // all of window
  76.                     ((var_code & 3) == 0) )    // document type
  77.         {
  78.             for (res_index = 1; ; ++res_index)
  79.             {
  80.                 code_h = GetIndResource('OAPd', res_index);
  81.                 if (code_h == NIL)
  82.                     break;
  83.                 HLock( code_h );
  84.                 OAPd_p = (OAPd_proc) StripAddress(*code_h);
  85.                 (*OAPd_p)( the_window );
  86.             }
  87.         }
  88.         
  89.         SetZone( save_zone );
  90.     }
  91.     
  92.     asm {
  93.         moveA.L        save_A4, A4
  94.     }
  95.     return( retval );
  96. }
  97.  
  98. /*    -------------------------------------------
  99.         No_ResEdit_danger    If the host application
  100.                                                 is being edited by ResEdit
  101.                                                 rather than executing
  102.         normally, we do not want this WDEF to
  103.         install any patches.
  104.         -------------------------------------------
  105. */
  106. Boolean No_ResEdit_danger( void )
  107. {
  108.     Handle        my_h;
  109.     short            my_resfile;
  110.     
  111.     my_resfile = -1;
  112.     my_h = RecoverHandle( (Ptr) main );
  113.     if (my_h != NIL)
  114.         my_resfile = HomeResFile( my_h );
  115.     return (my_resfile == CurApRefNum) ||
  116.         (CurApRefNum == 2);
  117. }
  118.